package com.leyou.user.web;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.interceptors.LoginInterceptor;
import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/info")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 校验用户名或手机号是否已经存在
     *
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> checkData(
            @PathVariable("data") String data,
            @PathVariable("type") Integer type) {

        return ResponseEntity.ok(this.userService.checkData(data, type));
    }

    /**
     * 发送短信验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendVerifyCode(String phone) {

        this.userService.sendVerifyCode(phone);
        return ResponseEntity.ok().build();
    }

    /**
     * 用户注册
     *
     * @param user
     * @param code
     * @return
     * @Valid 表示开启拦截支持
     */
    @PostMapping
    public ResponseEntity<Void> createUser(
            @Valid User user,
            @RequestParam("code") String code) {

        this.userService.createUser(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名和密码查询用户信息
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public ResponseEntity<UserDTO> queryUser(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        return ResponseEntity.ok(this.userService.queryUser(username, password));
    }

    /**
     * 获取一个cookie名称叫LY_TOKEN
     *
     * @param token
     * @return
     */
    @GetMapping("/me")
    public ResponseEntity<UserDetail> checkLogin(@CookieValue("LY_TOKEN") String token) {
        return ResponseEntity.ok(UserContext.getUser());
    }
}
